package dshop.model.member;

import java.util.Date;
import java.util.List;

import com.jfinal.plugin.activerecord.Record;

import appbase.jfinal.ext.BaseModel;
import dshop.AppConst;

public class MemberVip extends BaseModel<MemberVip>{
	
	private static final long serialVersionUID = -6967680902549117464L;
	
	public static final MemberVip dao = new MemberVip();

	//0 普通会员；1 一星会员；2 二星会员
	public static final int level_0 = 0;
	public static final int level_1 = 1;
	public static final int level_2 = 2;
	
	/**
	 * @return [{ level, start_date_time, end_date_time, isVipValid }]
	 */
	public static void setVipInfoToRs(List<Record> list, String accountIdColumn) 
	{
		if (list == null || list.isEmpty())
		{
			return;
		}
		
		dao.mergeColumnsToRs(list, accountIdColumn, "account_id", "account_id, level, start_date_time, end_date_time", "level, start_date_time, end_date_time", null);
		
		Date nowTime = new Date();
		for (Record r : list) 
		{
			Date startTime = r.getDate("start_date_time"); 
			Date endTime = r.getDate("end_date_time");
			if (startTime == null || endTime == null)
			{
				r.set("isVip", AppConst.STATUS_VAL_NO);
				continue;
			}
			
			r.set("isVip", AppConst.STATUS_VAL_YES);
			if (nowTime.after(startTime) && nowTime.before(endTime))
			{
				r.set("isVipValid", AppConst.STATUS_VAL_YES);
			}else 
			{
				r.set("isVipValid", AppConst.STATUS_VAL_NO);
			}
		}
	}

	
	public static int getLevelOf(int accountId) {
		MemberVip vip = dao.findByIdLoadColumns(accountId, "level");
		if (vip == null)
		{
			return level_0;
		}
		return Integer.valueOf(vip.get("level").toString());
	}

	
	
}
